package com.zte.claa.inficombo.csa.app.service.devpower;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;


/**
 * 电压到电量的转换器
 * @author 0283000170
 *
 */
public class DevPowerConverter implements Serializable{
	
	private static final long serialVersionUID = 1L;
		
	private static Set<Integer> gears = new HashSet<>();
	//支持的刻度
	private static final int GEAR1 = 1;
	private static final int GEAR2 = 2;
	private static final int GEAR5 = 5;
	private static final int GEAR10 = 10;
	//满量程
	private static final int FULL_SCALE = 100;	
	//高限
	private float ul;	
	//低限
	private float dl;
	//档位间隔:eg 5---->每次变化5个单位,如20%,25%,30%
	private int gear;
	//最小间隔
	private float interval;
	//总档位数
	private int number;
	//满电量(毫安时)
	private int fullpowercap;
			
	public DevPowerConverter(){			
	}
	
	
	public DevPowerConverter(float ul , float dl , int gear , int fullpowercap){		
		gears.add(GEAR1);
		gears.add(GEAR2);
		gears.add(GEAR5);
		gears.add(GEAR10);
		
		this.ul = ul;
		this.dl = dl;
		
		if(!gears.contains(gear))
			gear = GEAR10;
		
		this.gear = gear;
		this.number = FULL_SCALE / gear;
		this.interval = (ul - dl) / number;
		
		this.fullpowercap = fullpowercap;		
	}
	

	//当前电压到电量百分比换算	
	public int v2qConverter(double input) throws Exception{
		if(ul < dl)
			throw new IllegalArgumentException("ul must bigger then dl.");
								
		if(input <= dl){
			return gear;
		}
		if(input >= ul){
			return FULL_SCALE;
		}			
		 return (int)(Math.floor((input - dl) / interval) + 1) * gear;		
	}
		
	
	//当前累计用电mAh到电量百分比换算
	public int a2qConverter(double input) throws Exception{
		if(fullpowercap <= 0)
			throw new IllegalArgumentException("full power capacity is illegal.");
		
		if(input <= 0)
			return 0;
		return (int)((fullpowercap - (input % fullpowercap)) * FULL_SCALE / fullpowercap);
	}

	
	//电压等级到电量百分比换算
	public int l2qConverter(double input) throws Exception{
		int ret = 10;
		
		if(input <= 0){			
			ret = 10;
		}else if(input == 1){
			ret = 30;
		}else if(input == 2){
			ret = 60;
		}else if(input == 3){
			ret = 90;
		}else{
			ret = 10;
		}		
		return ret;		
	}

		

	public float getUl() {
		return ul;
	}


	public void setUl(float ul) {
		this.ul = ul;
	}


	public float getDl() {
		return dl;
	}


	public void setDl(float dl) {
		this.dl = dl;
	}


	public int getGear() {
		return gear;
	}


	public void setGear(int gear) {
		this.gear = gear;
	}


	public float getInterval() {
		return interval;
	}


	public void setInterval(float interval) {
		this.interval = interval;
	}


	public int getNumber() {
		return number;
	}


	public void setNumber(int number) {
		this.number = number;
	}


	public int getFullpowercap() {
		return fullpowercap;
	}


	public void setFullpowercap(int fullpowercap) {
		this.fullpowercap = fullpowercap;
	}
		
}



